//
//  EmoticonsGroup.swift
//  MyWeiBo
//
//  Created by mac on 15/3/13.
//  Copyright (c) 2015年 gpr. All rights reserved.
//

import UIKit

class EmoticonsGroup: BaseObject {
    ///  表情组名称
    var emoticon_group_name: String?
    ///  表情组标示符
    var emoticon_group_identifier: String?
    ///  表情符标示符
    var emoticon_group_type: Int = 0
    ///  表情组所在的目录名
    var emoticon_group_path: String? {
        didSet {
            let fileName = emoticon_group_path!.stringByAppendingPathComponent("info.plist")
            emoticon_info_path = NSBundle.mainBundle().pathForResource(fileName, ofType: nil)
        }
    }
     /// 表情标示符映射表,该属性主要用于方便显示
    var emotionsMapping: [String: Emoticon]?
    
    /// 记录每个表情组的 info.plist 的全路径名
    var emoticon_info_path: String? {
        didSet {
            let dict = NSDictionary(contentsOfFile: emoticon_info_path!)
            let array = dict!["emoticon_group_emoticons"] as! [[String: AnyObject]]
            self.loadEmotionsWithArray(array)
        }
    }
    
    func loadEmotionsWithArray(array: [[String: AnyObject]]) {
        var emotions = [Emoticon]()
        var emotionDict = [String: Emoticon]()
        let page = array.count / 20 + ( array.count % 20 == 0 ? 0 : 1 )
        let more = array.count % 20
        let count = page * 21
        var index = 1
        for item in array {
            if index % 21 == 0 {
                let emotion = Emoticon()
                emotion.isDelete = true
                emotions.append(emotion)
                index++
            }
            let emotion = Emoticon.objectWithDictionary(item) as! Emoticon
            emotion.folderName = emoticon_group_path
            emotions.append(emotion)
            if emotion.type != 1 {
                emotionDict[emotion.chs!] = emotion
            }
            index++
        }
        
        let left = count - index + 1
        for i in 0..<left {
            let emotion = Emoticon()
            emotion.isNone = true
            emotions.append(emotion)
        }
        emotions.last!.isDelete = true
        emotions.last!.isNone = false
        self.emotions = emotions
        self.emotionsMapping = emotionDict
    }
    
    /// 表情组
    var emotions: [Emoticon]?
}

class Emoticon: BaseObject {
    ///  简体表情中文标示符
    var chs: String?
    ///  繁体表情中文标示符
    var cht: String?
    ///  gif表情的文件名,如果该表情不支持 gif 则为空
    var gif: String?
    ///  普通的 png 表情文件名
    var png: String?
    ///  表情的类型 0 标示普通图片表情, 1标示emoji表情
    var type: Int = 0 {
        didSet {
            isEmoji = type == 1
        }
    }
    /// 目录名称
    var folderName: String? {
        didSet {
            if type == 0 {            
                self.imagePath = NSBundle.mainBundle().pathForResource(folderName, ofType: nil)?.stringByAppendingPathComponent(png!)
            }
        }
    }
    /// 用来标识是否删除图标
    var isDelete: Bool = false
    /// 用来标志是否 emoji 表情
    var isEmoji: Bool = false
    /// 用来标志该 emoticon 是用来占位置的
    var isNone: Bool = false
    
    /// 表情的路径名,对于非 emoji 表情该属性有值
    var imagePath: String?
    
    /// emoji 表情专用
    var code: String? {
        didSet {
            // 目标: 讲十六进制字符串,转化为十六进制数字,再以十六进制字符串转化为字符
            let scanner = NSScanner(string: code!)
            var strValue: UInt32 = 0
            scanner.scanHexInt(&strValue)
            
            self.emoji = "\( Character(UnicodeScalar(UnicodeScalar(strValue))) )"
        }
    }
    ///  最终显示在界面上使用此字段
    var emoji: String? 
}